﻿(function ($) {
    function _RECPFMSApp() {
        this.m_MainForm = null;
        this.m_TopPanel = null;
        this.m_LeftPanel = null;
        this.m_ContentPanel = null;
        this.m_ExtMenus = null;

        // main menus
        this.m_Menus = new Array();
        this.m_MenuIndex = 0;

        this.GetCurrentMenu = function () {
            if (this.m_MenuIndex >= 0) {
                return this.m_Menus[this.m_MenuIndex];
            }
            return null;
        }

        this.Init = function () {
            this.m_MainForm = $("#main_layout");
            this.m_TopPanel = this.m_MainForm.find(".TopPanel");
            this.m_LeftPanel = this.m_MainForm.find(".LeftPanel");
            this.m_ContentPanel = this.m_MainForm.find(".ContentPanel");
            this.m_ExtMenus = $("#ext-menus");

            this._InitUI();
            this._LoadWorkSpace();
        };

        this._InitUI = function () {
            this._LoadMainMenu();
            this._InitTopUserMenuEvent();
        };

        // 加载右上角的用户功能菜单的事件
        this._InitTopUserMenuEvent = function () {
            //expand west panel
            this.m_TopPanel.find(".RPanel .config").click($.App.ToggleConfig);

            this.m_TopPanel.find(".RPanel .PersonalInfo").click(function () {
                var menu = $("#user-menu-ext");
                var p = $(this).position();
                menu.css("left", p.left);
                menu.css("top", p.top + $(this).height());
                menu.css("width", $(this).width() + 20);
                menu.show(500);
                return false;
            });

            $(document.body).click(function () {
                $("#user-menu-ext").hide(500);
                $.App.m_ExtMenus.hide(100);
            });

            $("#user-menu-ext").find("li").each(function () {
                switch ($(this).attr("id")) {
                    case "user-settting":
                        $(this).click($.App.OpenAccountProfile);
                        break;
                    case "exit-system":
                        $(this).click($.App.Logoff);
                        break;
                }
            });

            // user profile menu
            this.m_Menus.push({
                index: this.m_Menus.length,
                menu: {
                    Id: 10000000,
                    ParentId: 0,
                    Name: '个人设置',
                    Url: "~/Account/UserProfile",
                },
                menuItem: null,
                menuMenus: null,
                menuPage: null,
            });
        };

        ///加载顶部主菜单
        this._LoadMainMenu = function () {
            $.ajax({
                async: false,
                url: "~/Home/GetMainMenus",
                success: function (menus) {
                    if (menus != null && menus.length > 0) {
                        if (menus.length > 1) {
                            var mainMenu = $.App.m_TopPanel.find(".top-main-menu");
                            var mainMenuW = mainMenu.width();
                            var totalMenuW = 0;
                            var i = 0;
                            for (i = 0; i < menus.length; i++) {
                                var item = $('<li class="menu-item" index="' + i + '" id="' + menus[i].Id + '"><i class="fa ' + menus[i].IconClassName + ' icon"></i><span>' + menus[i].Name + '</span></li>');
                                item.appendTo(mainMenu);
                                item.click(function () {
                                    var index = parseInt($(this).attr("index"));
                                    $.App.m_MenuIndex = index;

                                    for (var t = 0; t < $.App.m_Menus.length; t++) {
                                        var m = $.App.m_Menus[t];
                                        if (m.menuItem != null) { m.menuItem.removeClass("menu-item-active"); }
                                        if (m.menuMenus != null) { m.menuMenus.hide(); }
                                        if (m.menuPage != null) { m.menuPage.hide(); }
                                    }

                                    $(this).addClass("menu-item-active");
                                    var menu = $.App.m_Menus[index];
                                    if (menu.menuMenus != null) {
                                        menu.menuMenus.show();
                                        menu.menuPage.show();
                                    } else {
                                        $.App._LoadLeftMenu(menu);
                                    }
                                });
                                $.App.m_Menus.push({
                                    index: i,
                                    menu: menus[i],
                                    menuItem: item,
                                    menuMenus: null,
                                    menuPage: null,
                                });
                                totalMenuW += item.width();
                                if (totalMenuW + 350 > mainMenuW && i + 1 < menus.length) {
                                    var extMenuBtn = $('<li class="menu-item" style="min-width:0px"><i class="fa fa-angle-down icon"></i></li>');
                                    extMenuBtn.appendTo(mainMenu);
                                    extMenuBtn.click(function () {
                                        $.App.m_ExtMenus.css("left", mainMenu.offset().left);
                                        $.App.m_ExtMenus.css("top", mainMenu.height());
                                        $.App.m_ExtMenus.css("width", mainMenu.width());
                                        $.App.m_ExtMenus.show(200);
                                        return false;
                                    });

                                    for (i++; i < menus.length; i++) {
                                        var extMenu = $('<li class="menu-item" index="' + i + '" id="' + menus[i].Id + '"><i class="fa ' + menus[i].IconClassName + ' icon"></i><span>' + menus[i].Name + '</span></li>');
                                        extMenu.appendTo($.App.m_ExtMenus.find(".main-menus"));
                                        extMenu.click(function () {
                                            var index = parseInt($(this).attr("index"));
                                            $.App.m_MenuIndex = index;

                                            for (var t = 0; t < $.App.m_Menus.length; t++) {
                                                var m = $.App.m_Menus[t];
                                                if (m.menuMenus != null) { m.menuMenus.hide(); }
                                                if (m.menuPage != null) { m.menuPage.hide(); }
                                            }

                                            var menu = $.App.m_Menus[index];
                                            if (menu.menuMenus != null) {
                                                menu.menuMenus.show();
                                                menu.menuPage.show();
                                            } else {
                                                $.App._LoadLeftMenu(menu);
                                            }
                                        });
                                        $.App.m_Menus.push({
                                            index: i,
                                            menu: menus[i],
                                            menuItem: extMenu,
                                            menuMenus: null,
                                            menuPage: null,
                                        });
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            });
        }

        ///加载ajax菜单
        this._LoadLeftMenu = function (menu) {
            if (menu != null && menu.menu != null) {
                if (!$.IsNullOrEmpty(menu.menu.Url)) {
                    $.ajax({
                        url: menu.menu.Url + "?ID=" + menu.menu.Id,
                        success: function (content) {
                            var page = $(content);
                            menu.menuMenus = page.find(".LeftPanel");
                            menu.menuPage = page.find(".ContentPanel");

                            menu.menuMenus.appendTo($.App.m_LeftPanel);
                            menu.menuPage.appendTo($.App.m_ContentPanel);
                            $.parser.parse(menu.menuMenus);
                            $.parser.parse(menu.menuPage);
                        }
                    });
                }
            }
        };
        //// -----  end init menus ------


        // 加载内容区域
        this._LoadContentPanel = function (e) {
            var m = $(e);
            var menu = {
                Url: m.attr("src"),
                Id: m.attr("id"),
                Code: m.attr("code"),
                Name: m.find("span").text()
            };
            if (menu.Url == "null") { menu.Url = null; }
            if (!$.IsNullOrEmpty(menu.Url)) {
                var url = $.UrlAddParameter($.UrlAddParameter(menu.Url, "menuid", menu.Id), "code", menu.Code);
                $.ajax({
                    url: url,
                    success: function (c) {
                        var menu = $.App.GetCurrentMenu();
                        if (menu != null) {
                            menu.menuPage.remove();
                            menu.menuPage = $(c);
                            menu.menuPage.appendTo($.App.m_ContentPanel);
                            $.parser.parse(menu.menuPage);
                        }
                    },
                    error: function (e) {
                        ShowAjaxError(e);
                    }
                });
            }
        };

        //加载工作台
        this._LoadWorkSpace = function () {
            this._LoadLeftMenu(this.m_Menus[0]);
        };

        this.OpenAccountProfile = function () {
            debugger;

            var index = $.App.m_Menus.length - 1;
            $.App.m_MenuIndex = index;

            for (var t = 0; t < $.App.m_Menus.length; t++) {
                var m = $.App.m_Menus[t];
                if (m.menuMenus != null) { m.menuMenus.hide(); }
                if (m.menuPage != null) { m.menuPage.hide(); }
            }

            var menu = $.App.m_Menus[index];
            if (menu.menuMenus != null) {
                menu.menuMenus.show();
                menu.menuPage.show();
            } else {
                $.App._LoadLeftMenu(menu);
            }
        };

        this.Logoff = function () {
            location.href = $.Content("~/Account/Logout");
        };
    }

    $.App = new _RECPFMSApp();
})(jQuery);

$(document).ready(function () {
    $.App.Init();
});

